package ar.edu.itba.pod.Legajo47342;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ConnectException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.rmi.RemoteException;
import java.util.concurrent.TimeUnit;

import ar.edu.itba.pod.simul.ObjectFactoryAlternative;
import ar.edu.itba.pod.simul.communication.ConnectionManager;
import ar.edu.itba.pod.simul.market.Market;
import ar.edu.itba.pod.simul.market.MarketManager;
import ar.edu.itba.pod.simul.market.Resource;
import ar.edu.itba.pod.simul.simulation.SimulationManager;
import ar.edu.itba.pod.simul.ui.ConsoleFeedbackCallback;
import ar.edu.itba.pod.simul.ui.FeedbackCallback;
import ar.edu.itba.pod.simul.ui.FeedbackMarketManager;
import ar.edu.itba.pod.simul.ui.FeedbackSimulationManager;
import ar.edu.itba.pod.simul.units.SimpleConsumer;
import ar.edu.itba.pod.simul.units.SimpleProducer;

public class NodeStartup {

	//private static Node node;
	private static ObjectFactoryAlternative factory;
	
	public static void main(String[] args) {
		
		factory = new ObjectFactoryAlternativeImpl();

		if(menu() == 1){
			return;
		}
		
		//epezar la simulacion
		FeedbackCallback callback = new ConsoleFeedbackCallback();
		
		MarketManager marketM = factory.getMarketManager();
		marketM = new FeedbackMarketManager(callback, marketM);
		
		marketM.start();
		
		SimulationManager simulationM = factory.getSimulationManager();
		simulationM = new FeedbackSimulationManager(callback, simulationM);
		
		simulationM.register(Market.class, marketM.market());
		System.out.println("Starting simulation");
		simulationM.start();
		
		Resource copper = new Resource("Mineral", "Copper");
		Resource pigIron = new Resource("Mineral", "Pig Iron");
		Resource steel = new Resource("Alloy", "Steel");
		
		Integer cProd = 0;
		Integer pProd = 0;
		Integer sProd = 0;
		Integer cCons = 0;
		Integer pCons = 0;
		Integer sCons = 0;
		
		int op;
		
		do {
			op = menu2();
			switch (op) {
			case 1:
				//copper producer 5 cada 1 hora
				simulationM.addAgent(SimpleProducer.named("Copper P: " + cProd)
						.producing(5).of(copper).every(6, TimeUnit.HOURS)
						.build());
				cProd++;
				break;
			case 2:
				//copper consumer 5 cada 1 hora
				simulationM.addAgent(SimpleConsumer.named("Copper C: " + cCons)
						.consuming(8).of(copper).every(5, TimeUnit.HOURS)
						.build());
				cCons++;
				break;
			case 3:
				factory.disconnect();
				break;

			default:
				System.out.println("Error reading from stdin");
				break;
			}
		} while (op!=3);
		System.out.println("Terminando el programa");
		
		return;
	}

	private static int menu2() {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		System.out.println("\n\nAvailable Commands:\n1 Copper Producer 5/Hour\n2 Copper Consumer 5/Hour\n3 disconnect\n\n");
		
		while(true){
			System.out.println("enter command:\n");
			
			String comm;
			
			try {
				comm = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
				return 0;
			}
			
			if (comm.startsWith("1")) {
				
				return 1;
								
			}else if (comm.startsWith("2")) {
				
				return 2;
				
			}else if (comm.startsWith("3")) {
				
				return 3;
			}
			
		}
		
	}

	private static int menu() {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		System.out.println("Available Commands:\n1 createGroup\n2 connectToGroup\n3 disconnect\nquit\n\n");
		boolean salir = false;
		
		while(!salir){
			System.out.println("enter command:\n");
			
			String comm;
			
			try {
				comm = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
				return 1;
			}
			
			if (comm.startsWith("1")) {
				
				try {
					factory.createGroup();
				} catch (ConnectException e) {
					e.printStackTrace();
				}
				salir = true;
								
			}else if (comm.startsWith("2")) {
				
				String host = comm.split(" ")[1];
				System.out.println("connecting to: "+host);
				
				try {
					factory.connectToGroup(host);
				} catch (ConnectException e) {
					e.printStackTrace();
				}
				salir = true;
				
			}else if (comm.startsWith("3")) {
				
				factory.disconnect();
				salir = true;
			}else if (comm.startsWith("quit")) {
				return 1;
			}
		}
		
		return 0;
		
	}

}
